package com.example.driving.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.driving.base.BaseContext;
import com.example.driving.base.BaseFragment;
import com.example.driving.base.Data;
import com.example.driving.databinding.FragmentLoginBinding;
import com.example.driving.databinding.FragmentMineBinding;
import com.example.driving.model.ExamQuestionBean;
import com.example.driving.model.UserBean;
import com.example.driving.ui.LearnActivity;
import com.example.driving.ui.MainActivity;
import com.example.driving.ui.TabMineFragment;
import com.example.driving.utils.NetUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginFragment extends BaseFragment<FragmentLoginBinding> {

    private FragmentLoginBinding mBinding;

    @Override
    public FragmentLoginBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        mBinding = FragmentLoginBinding.inflate(inflater, container, false);
        return mBinding;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        //注册
        mBinding.text.setOnClickListener(v -> {
            EventBus.getDefault().post(new UserFragmentEvent(1));
        });
        //找回密码
        mBinding.text2.setOnClickListener(v ->{
            EventBus.getDefault().post(new UserFragmentEvent(2));
        });
        mBinding.button.setOnClickListener(v->{
            UserBean userBean = new UserBean();
            String account = mBinding.editText.getText().toString();
            String password = mBinding.editText2.getText().toString();
            userBean.setUserAccount(account);
            userBean.setPassword(password);
            Call<Data<UserBean>> login = NetUtils.get().Login(userBean);

            login.enqueue(new Callback<Data<UserBean>>() {
                @Override
                public void onResponse(Call<Data<UserBean>> call, Response<Data<UserBean>> response) {
                    Data<UserBean> body = response.body();
                    UserBean data = body.getData();

                    if (data != null) {
                        System.out.println(data.getDescription());

                        TabMineFragment.userBean = data;
                        BaseContext.setCurrentId(data.getId());
                        Intent startIntent = new Intent(getContext(), MainActivity.class);
                        startActivity(startIntent);
                    }else {
                        String description = response.body().getDescription();
                        System.out.println(description);
                        mBinding.editText.setError(description);
                    }
                }

                @Override
                public void onFailure(Call<Data<UserBean>> call, Throwable t) {

                }
            });

        });
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onBaseClick(View v) {

    }

    public void sendIndex(mCallBack callBack){
        callBack.sentFragmentIndex(1);
    }

    public interface mCallBack{
        void sentFragmentIndex(int index);
    }
}
